<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\db\Builder;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    public function getArticle()
    {
        $article = Db::name('rui_article')->field('id,title,image,xtitle,create_at')->where('is_del',0)->select();
        foreach ($article as &$v) {
            $v['image'] = 'https://shanghaihuxi.cn/' . $v['image'];
        }

        $banner = Db::name('rui_article')->where('banner', 1)->field('id,title,image,xtitle,create_at')->select();
        foreach ($banner as &$v) {
            $v['image'] = 'https://shanghaihuxi.cn/' . $v['image'];
        }

        $data['article'] = $article;
        $data['banner'] = $banner;

        $this->success('请求成功', $data);
    }

    public function getArticleDetail()
    {
        $info = Db::name('rui_article')->where('id', input('id'))->find();
        $this->success('请求成功', $info);
    }

    public function location()
    {
// 设定API密钥和请求参数
        $amapKey = '0e3d775a101d482bbed5253d27e4fef7'; // 替换为你的API密钥
//        $longitude  = '117.7535253' ?? null; // 获取纬度参数
//        $latitude = '36.746374' ?? null; // 获取经度参数

        $longitude = input('longitude'); // 获取纬度参数
        $latitude = input('latitude'); // 获取经度参数

// 检查经纬度参数是否有效
        if (!$latitude || !$longitude) {
            header('HTTP/1.1 400 Bad Request');
            echo '请提供有效的经纬度参数！';
            exit;
        }

// 逆地理编码API的URL模板
//        $url = "https://restapi.amap.com/v3/geocode/regeo?location={$latitude},{$longitude}&key={$amapKey}&output=json&pois=0";

        $url = "https://restapi.amap.com/v3/geocode/regeo?output=xml&location={$longitude},{$latitude}&key={$amapKey}&radius=1000&extensions=all";

// 发起HTTP请求获取结果
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);

        $xml = simplexml_load_string($output);
        $json = json_encode($xml);
        $array = json_decode($json, true);

// 将JSON结果转换为PHP数组或对象
//        $result = json_decode($output, true);

        $this->success('success', $array['regeocode']);
    }

    public function getYujingDetail()
    {
        $list = db('rui_yujing')->where('id', input('id'))->find();

        $this->success('提交成功', $list);
    }

    public function daka()
    {
        $data['user_id'] = input('user_id');
        $data['area'] = input('area');
        $data['create_at'] = date('Y-m-d H:i:s');
        $data['city'] = input('city');
        $list = db('rui_daka')->insert(
            $data
        );

        $this->success('提交成功');
    }

    public function getfenbao()
    {

        $list = db('rui_company')->select();

        $this->success('成功', $list);
    }

    public function getshengfen()
    {
        $list = db('rui_province')->field('id as value,name as label')->select();

        $this->success('成功', $list);
    }

    public function getshengfen2()
    {
        $list = db('rui_province')->field('id as value,name as label')->select();
        array_unshift($list, ['value'=>-1,'label'=>'全部']);

        $this->success('成功', $list);
    }

    public function getdakarenyuan()
    {
        $list = db('user')
            ->where('id', '<>', 1)
            ->where('shenfen', '>',0)
            ->field('id,nickname');

        if (!empty(input('gongsi_id'))) {
            $list = $list->where('company_id', input('gongsi_id'));
        }

        if (!empty(input('shengfen_id')) and input('shengfen_id') > 0) {
            $list = $list->where('province_id', input('shengfen_id'));
        }

        if (!empty(input('nickname'))) {
            $search = input('nickname');

            $list = $list->where('nickname', 'like', "%{$search}%");
        }

        $list = $list->column('id');
        $list2 = db('user')->field('id,nickname')->where('id', 'in', $list)->select();

        foreach ($list2 as &$v) {
            //最新打卡城市
            $daka = db('rui_daka')->where('user_id', $v['id'])->order('id desc');

            if (!empty(input('start_time'))) {
                $daka = $daka->where('create_at', '>=', input('start_time'));
            }

            if (!empty(input('end_time'))) {
                $daka = $daka->where('create_at', '<=', input('end_time'));
            }

            $v['city'] = $daka->find()['city'] ?? '未打卡';

            //出勤天数
            $dakaRecords = db('rui_daka')->where('user_id', $v['id'])
//                ->whereBetweenTime('create_at', $startTime, $endTime)
                ->field('DATE(create_at) as date, COUNT(*) as count')
                ->group('DATE(create_at)')
                ->having('count >= 2');

            if (!empty(input('start_time'))) {
                $dakaRecords = $dakaRecords->where('create_at', '>=', input('start_time'));
            }

            if (!empty(input('end_time'))) {
                $dakaRecords = $dakaRecords->where('create_at', '<=', input('end_time'));
            }

            $dakaRecords = $dakaRecords->count();


            $v['chuqin'] = $dakaRecords . '天';
            //提交工单数
            $count = db('csmwj_userpaper')
                ->where('csmwj_weixinuser_id', $v['id'])
                ->where('status', 'normal')
                ->where('answerstatus', '=', 'FINISH');

            if (!empty(input('start_time'))) {
                $count = $count->where('createtime', '>=', strtotime(input('start_time')));
            }

            if (!empty(input('end_time'))) {
                $count = $count->where('createtime', '<=', strtotime(input('end_time')));
            }

            $count = $count->count();

            $v['gongdan'] = $count . '次';
        }

        $this->success('提交成功', $list2);
    }


    public function getdaka()
    {
        $list = db('rui_daka')->alias('a')
            ->join('user c', 'c.id = a.user_id', 'left')
            ->order('a.id desc')
//            ->join('rui_company b', 'b.id = c.company_id', 'left')
            ->field('c.nickname,a.area,a.create_at,a.city');

        if (!empty(input('gongsi_id'))) {
            $list = $list->where('c.company_id', input('gongsi_id'));
        }

        if (!empty(input('shengfen_id'))) {
            $list = $list->where('c.province_id', input('shengfen_id'));
        }

        if (!empty(input('user_id'))) {
            $list = $list->where('c.id', input('user_id'));
        }


        $list = $list->select();

        $this->success('提交成功', $list);
    }

    public function getBaoBiaoDetail()
    {
        $list = db('csmwj_userpaper')->where('id', input('id'))->find();
        $this->success('提交成功', $list);
    }

}
